package com.blog.controller.admin;

import com.blog.controller.BackendController;
import com.blog.model.Category;
import com.blog.model.CategoryLev;
import com.blog.service.CategoryService;
import com.blog.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin/category")
public class CategoryController extends BackendController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/indexx")
    public ModelAndView indexx(Category category){

        ModelAndView result = new ModelAndView("/admin/category/index2");
        List<Category> categoryList = categoryService.findAll(category);
        result.addObject("pageInfo", new PageInfo<Category>(categoryList));
        result.addObject("queryParam", category);
        result.addObject("page", category.getPage());
        result.addObject("rows", category.getRows());
        return result;
    }

    @GetMapping("")
    public String index(HttpServletRequest request, Category category){
        List<Category> categoryList = categoryService.selectAll(category);
        List<CategoryLev> backList = getTree(categoryList, false);

        request.setAttribute("queryParam", category);
        request.setAttribute("pageInfo", backList);
        return "admin/category/index";
    }

    @GetMapping("/add")
    public String add(Category category, HttpServletRequest request){
        List<Category> categoryList = categoryService.selectAll();
        List<CategoryLev> backList = getTree(categoryList, true);

        request.setAttribute("data", backList);
        return "admin/category/add";
    }

    @RequestMapping(value = "/addsave",method = RequestMethod.POST)
    public String addsave(Category category){
        category.setCount(0);
        categoryService.addCategory(category);
        return "redirect:/admin/category";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, HttpServletRequest request, Category category){
        Category info = categoryService.findOne(id);
        List<Category> categoryList = categoryService.selectAll();
        List<CategoryLev> backList = getTree(categoryList, true);

        request.setAttribute("categoryList", backList);
        request.setAttribute("info",info);
        return "admin/category/edit";
    }

    @RequestMapping(value = "/editsave",method = RequestMethod.POST)
    public String editsave(Category category){
        categoryService.editCategory(category);
        return "redirect:/admin/category";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult del(@Param("id") Integer id){
        try {
            categoryService.deleteCategory(id);
            return JsonResult.ok();
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 获取树结构
     * @param categoryList
     * @param prefix
     * @return
     */
    private List<CategoryLev> getTree(List<Category> categoryList, boolean prefix){
        List<CategoryLev> backList = new ArrayList<CategoryLev>();
        recMessageType(backList, categoryList, 0, 0);

        if(prefix){
            for (CategoryLev categoryLev : backList) {
                categoryLev.setName(setNBSP(categoryLev) + categoryLev.getName());
            }
        }

        return backList;
    }

    /**
     * 对无限分类集合 数组进行排序
     * @param mlist 要排序的集合
     * @param id    父id 为0开始
     * @param lev 层数 为了显示缩进 而添加的
     */
    private void recMessageType(List<CategoryLev> bList, List<Category> mlist, int id, int lev) {
        for (Category category : mlist) {
            if (category.getParentid() == id) {
                CategoryLev categoryLev = new CategoryLev();
                categoryLev.from(category);
                categoryLev.setLev(lev);
                bList.add(categoryLev);
                recMessageType(bList, mlist, category.getId(), lev + 1);
            }
        }
    }

    /**
     * 根据层级的关系处理 类别名称显示
     * @param categoryLev
     * @return
     */
    private String setNBSP(CategoryLev categoryLev) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < categoryLev.getLev(); i++) {
            sb.append("----");
        }
        return sb.toString();
    }

}

